<?php 

header("Content-type: image/jpeg");  


/**
* 生成验证码class
*/
class Code
{	
	public $witdh;
	public $height;//画布大小
	public $res;//画布
	public $num1;//设定一个循环小于值
	public $num2;//干扰线条值
	public $marin;//字体间距设置
	
	
	public function __construct($witdh=400,$height=200,$num1=4,$num2=100){
				$this->witdh=$witdh;
				$this->height=$height;
				$this->num1=$num1;
				$this->num2=$num2;
				//	构造，初始化
	}

	public function show()
	{
		$this->canvas();
		$this->fonts();
		$this->lines();
		$this->out();
		//封装函数
	}


public function canvas(){
		$this->res=imageCreateTrueColor($this->witdh,$this->height);
		$color=imagecolorallocate($this->res, 127, 127, 127);
		imageFill($this->res,0,0,$color);
		//生成画布，设置颜色，填充颜色
		
		
}


public function fonts(){
		//生成字母数字
		for ($i=0; $i <$this->num1 ; $i++) {

		$str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
		$code = $str[mt_rand(0,strlen($str)-1)];

		$this->marin = $i*80;
		$this->marin+=30;

		$color=imagecolorallocate($this->res,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//随机颜色
		imagettftext($this->res,$this->witdh/$this->num1 ,mt_rand(-5,10) ,$this->marin, mt_rand($this->height/2,$this->height),$color, './FireBlock.ttf', $code);
		//字母个数宽高与$witdh，$height相关联，改变画布大小，字体大小随之改变
		//保证字母位置在画布中央区域

		}
		
		
}
		
	
public function lines(){

		for ($j=0; $j <$this->num2 ; $j++) { 
		$color=imagecolorallocate($this->res,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
		imageline($this->res, mt_rand(0,$this->witdh), mt_rand(0,$this->height), rand(0,$this->witdh), rand(0,$this->height), $color);//线条
		}

		//生成干扰线条和点
}


public function out(){
	imagejpeg($this->res);//输出画布
}


}



 ?>